package com.sdkx.app.activitys;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.WayPointManager;
import com.sdkx.app.adapter.CurAirportListAdapter;
import com.sdkx.app.model.WayPoint;

import java.util.List;

public class SetCurrentAirportActivity extends AppCompatActivity {
    private final String TAG = SetCurrentAirportActivity.class.getSimpleName();
    private List<WayPoint> listDataSource;
    private CurAirportListAdapter adapter;
    private EditText searchEditText;
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 这句代码修复点击屏幕顶部不灵敏。
        getWindow().setFlags(1024, 1024);
        setContentView(R.layout.activity_set_current_airport);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar!=null){
            actionBar.setTitle("设置本机机场");
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return true;
    }

    private void initView(){
        radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                onRadioButtonCheckedChanged(i);
            }
        });
        searchEditText = (EditText)findViewById(R.id.editText_search);
        searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if(i == EditorInfo.IME_ACTION_SEARCH){
                    search();
                }
                return false;
            }
        });
        Button button_search = (Button)findViewById(R.id.button_search);
        button_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                search();
            }
        });
        ListView listView = (ListView)findViewById(R.id.listview);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                onListViewItemClick(view,i);
            }
        });
        listDataSource = getAllAirports(null);
        adapter = new CurAirportListAdapter(this,R.layout.pref_cur_airport_list_item,listDataSource);
        listView.setAdapter(adapter);
    }

    private void onRadioButtonCheckedChanged(int radioButtonId){
        listDataSource.clear();
        adapter.setSelectPosition(-1);
        switch (radioButtonId) {
            case R.id.radioBtn_all:
                listDataSource.addAll(getAllAirports(searchEditText.getText().toString()));
                break;
            case R.id.radioBtn_military:
                listDataSource.addAll(getMilitaryAirports(searchEditText.getText().toString()));
                break;
            case R.id.radioBtn_ais:
                listDataSource.addAll(getAisAirports(searchEditText.getText().toString()));
                break;
            case R.id.radioBtn_ais_military:
                listDataSource.addAll(getAisMilitaryAirports(searchEditText.getText().toString()));
                break;
            default:
                break;
        }
        adapter.notifyDataSetInvalidated();
    }

    private void search(){
        onRadioButtonCheckedChanged(radioGroup.getCheckedRadioButtonId());
    }

    private void onListViewItemClick(View view, final int position) {
        adapter.setSelectPosition(position);
        adapter.notifyDataSetInvalidated();
    }

    private List<WayPoint> getAllAirports(String keyword) {
        if (TextUtils.isEmpty(keyword))
            return WayPointManager.getItemsByTypes(new int[]{
                    WayPoint.WayPointType.MILITARY_AIRPORT.ordinal(),
                    WayPoint.WayPointType.AIS_AIRPORT.ordinal(),
                    WayPoint.WayPointType.AM_AIRPORT.ordinal()
            });
        else
            return WayPointManager.getItemsByTypesAndName(new int[]{
                    WayPoint.WayPointType.MILITARY_AIRPORT.ordinal(),
                    WayPoint.WayPointType.AIS_AIRPORT.ordinal(),
                    WayPoint.WayPointType.AM_AIRPORT.ordinal()
            }, keyword);
    }

    private List<WayPoint> getMilitaryAirports(String keyword) {
        if (TextUtils.isEmpty(keyword))
            return WayPointManager.getItemsByTypes(new int[]{
                    WayPoint.WayPointType.MILITARY_AIRPORT.ordinal()
            });
        else
            return WayPointManager.getItemsByTypesAndName(new int[]{
                    WayPoint.WayPointType.MILITARY_AIRPORT.ordinal()
            }, keyword);
    }

    private List<WayPoint> getAisAirports(String keyword) {
        if (TextUtils.isEmpty(keyword))
            return WayPointManager.getItemsByTypes(new int[]{
                    WayPoint.WayPointType.AIS_AIRPORT.ordinal()
            });
        else
            return WayPointManager.getItemsByTypesAndName(new int[]{
                    WayPoint.WayPointType.AIS_AIRPORT.ordinal()
            }, keyword);
    }

    private List<WayPoint> getAisMilitaryAirports(String keyword) {
        if (TextUtils.isEmpty(keyword))
            return WayPointManager.getItemsByTypes(new int[]{
                    WayPoint.WayPointType.AM_AIRPORT.ordinal()
            });
        else
            return WayPointManager.getItemsByTypesAndName(new int[]{
                    WayPoint.WayPointType.AM_AIRPORT.ordinal()
            }, keyword);
    }
}
